package org.softmed.rest.admin.aop;

import java.util.ArrayList;
import java.util.List;

import org.softmed.rest.config.Handler;
import org.softmed.rest.server.core.handler.HandlerProcessParameters;

public class Rule {
	Long id;
	List<Long> applications = new ArrayList<Long>();
	List<Long> modules = new ArrayList<Long>();
	List<Long> resources = new ArrayList<Long>();
	List<Long> httpMethods = new ArrayList<Long>();
	List<Long> mimeTypeHandlers = new ArrayList<Long>();
	List<Long> handlers = new ArrayList<Long>();

	List<String> applicationURIs = new ArrayList<String>();
	List<String> moduleURIs = new ArrayList<String>();
	List<String> resourceURIs = new ArrayList<String>();
	List<String> httpMethodNames = new ArrayList<String>();
	List<String> mimeTypes = new ArrayList<String>();
	List<String> paths = new ArrayList<String>();

	private Object readResolve() {
		applications = new ArrayList<Long>();
		modules = new ArrayList<Long>();
		resources = new ArrayList<Long>();
		httpMethods = new ArrayList<Long>();
		mimeTypeHandlers = new ArrayList<Long>();
		handlers = new ArrayList<Long>();

		applicationURIs = new ArrayList<String>();
		moduleURIs = new ArrayList<String>();
		resourceURIs = new ArrayList<String>();
		httpMethodNames = new ArrayList<String>();
		mimeTypes = new ArrayList<String>();
		paths = new ArrayList<String>();
	    return this;
	  }

	
	public boolean isApplicable(Handler handler,
			HandlerProcessParameters parameters) {

		if (applications.size() > 0
				&& !applications.contains(parameters.getApplication().getId()))
			return false;

		if (modules.size() > 0
				&& !modules.contains(parameters.getModule().getId()))
			return false;

		if (resources.size() > 0
				&& !resources.contains(parameters.getResource().getId()))
			return false;

		if (httpMethods.size() > 0
				&& (parameters.getHttpMethod() == null || !httpMethods
						.contains(parameters.getHttpMethod().getId())))
			return false;

		if (mimeTypeHandlers.size() > 0
				&& (parameters.getMimeTypeHandler() == null || !mimeTypeHandlers
						.contains(parameters.getMimeTypeHandler().getId())))
			return false;

		if (handlers.size() > 0 && !handlers.contains(handler.getId()))
			return false;

		// string hits
		if (applicationURIs.size() > 0
				&& (!applicationURIs.contains(parameters.getApplication()
						.getRelativeUri())))
			return false;

		if (moduleURIs.size() > 0
				&& (!moduleURIs.contains(parameters.getModule()
						.getRelativeUri())))
			return false;

		if (resourceURIs.size() > 0
				&& (!resourceURIs.contains(parameters.getResource()
						.getTargetUri())))
			return false;

		if (httpMethodNames.size() > 0
				&& (parameters.getHttpMethod() == null || !httpMethodNames
						.contains(parameters.getHttpMethod().getName())))
			return false;

		if (mimeTypes.size() > 0
				&& (parameters.getMimeTypeHandler() == null || !mimeTypes
						.contains(parameters.getMimeTypeHandler().getMimeType())))
			return false;

		if (paths.size() > 0 && (!paths.contains(handler.getPath())))
			return false;

		return true;
	}

	public void setApplications(List<Long> applications) {
		this.applications = applications;
	}

	public void setModules(List<Long> modules) {
		this.modules = modules;
	}

	public void setResources(List<Long> resources) {
		this.resources = resources;
	}

	public void setHttpMethods(List<Long> httpMethods) {
		this.httpMethods = httpMethods;
	}

	public void setMimeTypeHandlers(List<Long> mimeTypeHandlers) {
		this.mimeTypeHandlers = mimeTypeHandlers;
	}

	public void setHandlers(List<Long> handlers) {
		this.handlers = handlers;
	}

	public void setApplicationURIs(List<String> applicationURIs) {
		this.applicationURIs = applicationURIs;
	}

	public void setModuleURIs(List<String> moduleURIs) {
		this.moduleURIs = moduleURIs;
	}

	public void setResourceURIs(List<String> resourceURIs) {
		this.resourceURIs = resourceURIs;
	}

	public void setHttpMethodNames(List<String> httpMethodNames) {
		this.httpMethodNames = httpMethodNames;
	}

	public void setMimeTypes(List<String> mimeTypes) {
		this.mimeTypes = mimeTypes;
	}

	public void setPaths(List<String> paths) {
		this.paths = paths;
	}

	public List<Long> getApplications() {
		return applications;
	}

	public List<Long> getModules() {
		return modules;
	}

	public List<Long> getResources() {
		return resources;
	}

	public List<Long> getHttpMethods() {
		return httpMethods;
	}

	public List<Long> getMimeTypeHandlers() {
		return mimeTypeHandlers;
	}

	public List<Long> getHandlers() {
		return handlers;
	}

	public List<String> getApplicationURIs() {
		return applicationURIs;
	}

	public List<String> getModuleURIs() {
		return moduleURIs;
	}

	public List<String> getResourceURIs() {
		return resourceURIs;
	}

	public List<String> getHttpMethodNames() {
		return httpMethodNames;
	}

	public List<String> getMimeTypes() {
		return mimeTypes;
	}

	public List<String> getPaths() {
		return paths;
	}


	public Long getId() {
		return id;
	}


	public void setId(Long id) {
		this.id = id;
	}

}
